package com.hopes.bizz.basic.job;

/**
 * @author: ljh
 * @create: 2024/1/23
 */

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.hopes.bizz.basic.common.constant.CalendarConstant;
import com.hopes.bizz.basic.park.entity.BaseCalendarEntity;
import com.hopes.bizz.basic.park.service.BaseCalendarService;
import com.hopes.bizz.common.core.util.DateUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/6/16 14:59
 * @Description: 同步接下来一年日期
 * 每年的第一天执行*
 */
@Component
@Slf4j
public class BaseCalendarJob {
    @Autowired
    BaseCalendarService calendarService;

    @XxlJob("baseCalendarJobHandler")
    public void baseCalendarJobHandler() {
        DateTime firstDayByYear = DateUtil.beginOfDay(DateUtil.beginOfYear(new Date()));
        DateTime lastDayByYear = DateUtil.endOfYear(firstDayByYear);
        List<DateTime> dates = DateUtil.rangeToList(firstDayByYear, lastDayByYear, DateField.DAY_OF_YEAR);
        for (DateTime dateDate : dates) {
            if (calendarService.lambdaQuery().eq(BaseCalendarEntity::getDateDate,dateDate).one() == null) {
                BaseCalendarEntity entity = new BaseCalendarEntity();
                String dateStr = DateUtils.formatDate(dateDate, "yyyyMMdd");
                try {
                    Thread.sleep(1500);
                    String response = HttpUtil.get("https://www.mxnzp.com/api/holiday/single/" + dateStr +
                            "?ignoreHoliday=false&app_id=lrquqfrnknkkyesr&app_secret=VUFDS080M0tMMURjNjR2UEgva1VKQT09");
                    JSONObject result = JSONObject.parseObject(response);
                    JSONObject data = result.getJSONObject("data");
                    if (data == null) {
                        log.warn("查询" + dateStr + "日期为空！");
                        continue;
                    }
                    entity.setDateDate(dateDate);

                    int dateType = data.getIntValue("type");
                    if (dateType == 0) {
                        dateType = CalendarConstant.WEEKDAY;
                    } else {
                        dateType = CalendarConstant.HOLIDAY;
                    }
                    entity.setDateType(dateType);
                    if (data.getString("lunarCalendar").length() >= 4) {
                        entity.setChineseCalendar(data.getString("lunarCalendar").substring(2, 4));
                    } else {
                        entity.setChineseCalendar(data.getString("lunarCalendar"));
                    }

                    if (dateType == CalendarConstant.HOLIDAY) {
                        entity.setDateLabel("休");
                    } else {
                        entity.setDateLabel("班");
                    }

                    calendarService.save(entity);
                }
                catch (Exception e) {
                    log.error("查询" + dateStr + "日期失败！", e);
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
